{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 分类问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAEWCAYAAABliCz2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAbs0lEQVR4nO3dfZQU9Z3v8fdnQBEYQAlCeBAQNYCCesEsnrNJHG7UqPgYvRpioqLRqCTIicZldeO65MnN6kaTm1UhiaAhanyI4GpuNOpE4moiGB7MmoGoJBi9IMYHGLjC4Pf+0QW2OAMNdk1P9+/zOqfPVFdVd32/lH66+lc1NYoIzMys9tVVugAzM2sfDnwzs0Q48M3MEuHANzNLhAPfzCwRDnwzs0Q48M22IekmSV+rdB1m5SZfh2/lImkF0A/YXDT7IxHx8gd4zwbgJxEx6INVV50kzQJeioh/qnQtVv18hG/ldkJE1Bc9djnsy0FS50pu/4OQ1KnSNVhtceBbu5B0uKT/kvSGpMXZkfuWZZMkPSdpraQXJH0xm98d+AUwQNK67DFA0ixJ3yh6fYOkl4qer5D0D5KWAM2SOmevu0fSq5JelDRlO7Vuff8t7y3pckmrJb0i6WRJx0laJulvkq4oeu3Vku6WdGfWzzOSDilaPlJSY/bv8AdJJ26z3RslPSipGTgPOBO4POv9/my9aZKez97/vyWdUvQe50j6jaRrJb2e9Xps0fLekm6R9HK2/L6iZcdLWpTV9l+SDi55B1tVcOBb7iQNBB4AvgH0Bi4D7pG0d7bKauB4oCcwCfiupDER0QwcC7y8C98YJgITgD2Bd4D7gcXAQOCTwFRJnyrxvT4M7JG99ipgJvA5YCzwceAqScOK1j8JuCvr9afAfZJ2k7RbVsdDQF/gy8AcScOLXvtZ4JtAD+BWYA7wnaz3E7J1ns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wsEf8f3GMZBi6o5E8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.8947368421052632\n"
     ]
    }
   ],
   "source": [
    "from xgboost import XGBClassifier\n",
    "from xgboost import plot_importance\n",
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "\n",
    "X,y = load_iris(return_X_y=True) # iris 花\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, y)\n",
    "\n",
    "model = XGBClassifier(learning_rate=0.1,\n",
    "                      n_estimators=100,          # 树的个数--100棵树建立xgboost\n",
    "                      max_depth=6,               # 树的深度\n",
    "                      min_child_weight = 1,      # 叶子节点最小权重\n",
    "                      gamma=0.,                  # 惩罚项中叶子结点个数前的参数\n",
    "                      subsample=0.8,             # 随机选择80%样本建立决策树\n",
    "                      colsample_btree=0.8,       # 随机选择80%特征建立决策树\n",
    "                      objective='multi:softmax', # 指定损失函数\n",
    "                      scale_pos_weight=1         # 解决样本个数不平衡的问题\n",
    "                      )\n",
    "\n",
    "model.fit(x_train,y_train)\n",
    "\n",
    "# plot feature importance\n",
    "plot_importance(model)                                                            \n",
    "plt.show()\n",
    "\n",
    "# make prediction for test data\n",
    "y_pred = model.predict(x_test)\n",
    "\n",
    "# model evaluate\n",
    "accuracy = accuracy_score(y_test,y_pred)\n",
    "print(accuracy)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "XGBoost 参数一般分为三种：\n",
    "\n",
    "+ General parameters：参数控制在提升（boosting）过程中使用哪种booster，常用的booster有树模型（tree）和线性模型（linear model）。\n",
    "+ Booster parameters：这取决于使用哪种booster。\n",
    "+ Learning Task parameters：控制学习的场景，例如在回归问题中会使用不同的参数控制排序\n",
    "\n",
    "### 通用参数\n",
    "\n",
    "+ booster\n",
    "\n",
    "gbtree和gblinear \n",
    "\n",
    "+ silent\n",
    "\n",
    "默认 0，为1时模型运行不输出 \n",
    "\n",
    "+ nthread\n",
    "\n",
    "默认值为最大可能的线程数\n",
    "\n",
    "\n",
    "\n",
    "### Booster参数\n",
    "\n",
    "+ n_estimator\n",
    "\n",
    "生成的最大树的数目，也是最大的迭代次数\n",
    "\n",
    "+ learning_rate\n",
    "\n",
    "每一步迭代的步长 \n",
    "\n",
    "+ gamma\n",
    "\n",
    "默认 0，Gamma指定了节点分裂所需的最小损失函数下降值\n",
    "\n",
    "+ subsample\n",
    "\n",
    "默认 1，这个参数控制对于每棵树，随机采样的比例 \n",
    "\n",
    "+ colsample_bytree\n",
    "\n",
    "默认 1，用来控制每棵随机采样的列数的占比\n",
    "\n",
    "+ colsample_bylevel\n",
    "\n",
    "默认 1，每棵树每次节点分裂的时候列采样的比例\n",
    "\n",
    "+ max_depth\n",
    "\n",
    "默认 6，我们常用3-10之间的数字。这个值为树的最大深度。这个值是用来控制过拟合的。max_depth越大，模型学习的更加具体\n",
    "\n",
    "+ max_delta_step\n",
    "\n",
    "默认 0，这个参数限制了每棵树权重改变的最大步长，如果这个参数的值为0,则意味着没有约束\n",
    "\n",
    "+ lambda\n",
    "\n",
    "默认 0，权重的L2正则化项\n",
    "\n",
    "+ alpha\n",
    "\n",
    "默认 0，权重的L1正则化项\n",
    "\n",
    "+ scale_pos_weight\n",
    "\n",
    "默认 1，在各类别样本十分不平衡时，把这个参数设定为一个正值，可以使算法更快收敛。通常可以将其设置为负样本的数目与正样本数目的比值\n",
    "\n",
    "\n",
    "### 学习目标参数\n",
    "+ objective\n",
    "\n",
    "reg:linear– 线性回归\n",
    "\n",
    "reg:logistic – 逻辑回归\n",
    "\n",
    "binary:logistic – 二分类逻辑回归，输出为概率\n",
    "\n",
    "binary:logitraw – 二分类逻辑回归，输出的结果为wTx\n",
    "\n",
    "count:poisson – 计数问题的poisson回归，输出结果为poisson分布。在poisson回归中，max_delta_step的缺省值为0.7 (used to safeguard optimization)\n",
    "\n",
    "multi:softmax – 设置 XGBoost 使用softmax目标函数做多分类，需要设置参数num_class（类别个数）\n",
    "\n",
    "multi:softprob – 如同softmax，但是输出结果为ndata*nclass的向量，其中的值是每个数据分为每个类的概率\n",
    "\n",
    "+ eval_metric\n",
    "\n",
    "rmse: 均方根误差\n",
    "\n",
    "mae: 平均绝对值误差\n",
    "\n",
    "logloss: negative log-likelihood\n",
    "\n",
    "error: 二分类错误率。其值通过错误分类数目与全部分类数目比值得到。对于预测，预测值大于0.5被认为是正类，其它归为负类。 error@t: 不同的划分阈值可以通过 ‘t’进行设置\n",
    "\n",
    "merror: 多分类错误率，计算公式为(wrong cases)/(all cases)\n",
    "\n",
    "mlogloss: 多分类log损失\n",
    "\n",
    "auc: 曲线下的面积\n",
    "\n",
    "ndcg: Normalized Discounted Cumulative Gain\n",
    "\n",
    "map: 平均正确率\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " `scoring`参数选择 : https://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter\n",
    "\n",
    "![](https://tva1.sinaimg.cn/large/007S8ZIlly1gflclt71k4j30o40oo7ak.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调参函数\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "def modify_params(parameters):\n",
    "    #param_grid = {param : param_list}\n",
    "    kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)\n",
    "    model = XGBClassifier()\n",
    "    grid_search = GridSearchCV(model, parameters, scoring=\"accuracy\", n_jobs=-1, cv=kfold)\n",
    "    grid_result = grid_search.fit(x_train,y_train)\n",
    "    means = grid_result.cv_results_['mean_test_score']\n",
    "    stds = grid_result.cv_results_['std_test_score']\n",
    "    params = grid_result.cv_results_['params']\n",
    "    for mean, stdev, param in zip(means, stds, params):\n",
    "        print(\"mean:%f ,std:%f with: %r\" % (mean, stdev, param))\n",
    "    print(\"Best: %f using %s\" % (grid_result.best_score_, grid_result.best_params_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean:0.955357 ,std:0.060176 with: {'learning_rate': 0.0001}\n",
      "mean:0.955357 ,std:0.060176 with: {'learning_rate': 0.001}\n",
      "mean:0.955357 ,std:0.060176 with: {'learning_rate': 0.01}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.1}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.2}\n",
      "mean:0.955357 ,std:0.060176 with: {'learning_rate': 0.3}\n",
      "Best: 0.964286 using {'learning_rate': 0.1}\n"
     ]
    }
   ],
   "source": [
    "# 选择学习率\n",
    "learning_rate_param={\"learning_rate\" : [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3]}\n",
    "modify_params(learning_rate_param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean:0.955357 ,std:0.060176 with: {'n_estimators': 10}\n",
      "mean:0.964286 ,std:0.043630 with: {'n_estimators': 100}\n",
      "mean:0.964286 ,std:0.043630 with: {'n_estimators': 500}\n",
      "mean:0.964286 ,std:0.043630 with: {'n_estimators': 1000}\n",
      "Best: 0.964286 using {'n_estimators': 100}\n"
     ]
    }
   ],
   "source": [
    "# 选择树的个数\n",
    "n_estimators_param={\"n_estimators\" : [10, 100, 500, 1000]}\n",
    "modify_params(n_estimators_param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean:0.955357 ,std:0.060176 with: {'learning_rate': 0.01, 'n_estimators': 50}\n",
      "mean:0.955357 ,std:0.060176 with: {'learning_rate': 0.01, 'n_estimators': 100}\n",
      "mean:0.973214 ,std:0.041444 with: {'learning_rate': 0.01, 'n_estimators': 200}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.01, 'n_estimators': 300}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.01, 'n_estimators': 500}\n",
      "mean:0.955357 ,std:0.060176 with: {'learning_rate': 0.02, 'n_estimators': 50}\n",
      "mean:0.973214 ,std:0.041444 with: {'learning_rate': 0.02, 'n_estimators': 100}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.02, 'n_estimators': 200}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.02, 'n_estimators': 300}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.02, 'n_estimators': 500}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.05, 'n_estimators': 50}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.05, 'n_estimators': 100}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.05, 'n_estimators': 200}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.05, 'n_estimators': 300}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.05, 'n_estimators': 500}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.1, 'n_estimators': 50}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.1, 'n_estimators': 100}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.1, 'n_estimators': 200}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.1, 'n_estimators': 300}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.1, 'n_estimators': 500}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.15, 'n_estimators': 50}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.15, 'n_estimators': 100}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.15, 'n_estimators': 200}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.15, 'n_estimators': 300}\n",
      "mean:0.964286 ,std:0.043630 with: {'learning_rate': 0.15, 'n_estimators': 500}\n",
      "Best: 0.973214 using {'learning_rate': 0.01, 'n_estimators': 200}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hz/anaconda3/lib/python3.7/site-packages/sklearn/model_selection/_search.py:813: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.\n",
      "  DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "parameters = {\n",
    "    'learning_rate': [0.01, 0.02, 0.05, 0.1, 0.15],\n",
    "    'n_estimators': [50, 100, 200, 300, 500]\n",
    "}\n",
    "modify_params(parameters)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 回归问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12.872580643891542\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAAEWCAYAAABrDZDcAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3de3hU9bX/8feCqEUQEBGMUEQkQLhGsIAt1VAOIki9VI/KoYqCR/EG1Aumx2rVXqAV5GJ78ChYKV6PV7RWLApBfx68BAW8AKIlClRBEYUgYgjr98feiUNIhgCZ2ZmZz+t55snMd+89e81SZs3ee2Z9zd0REZHMVS/qAEREJFoqBCIiGU6FQEQkw6kQiIhkOBUCEZEMp0IgIp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222/ngQce4Ljjjqt4A/70009p1qwZTz/9NCecUOV7W8Kk6nwEZlZtIagr/xrOZx9OC2k+gvhStZ98sig/8SUrP1XNHzB+/Hg++OAD2rdvj7vzzDPP0KlTJ5o0acLnn39esW1+fj6TJk1KehFIV5EXAjNrApwM/DzqWEQkeaqaP+C0007jxz/+MVu2bMHd6dGjBzNmzIg61LQXeSEAzgL+4e7bog5ERJKnuvkDXnnllb1um25zBkct8vkI3P0+gknsRUQkAvplsYhIhlMhEBHJcCoEIiIZToVARCTDqRCIiGQ4FQIRqVZ1raLXrFlDnz59yMnJ4bzzzuPbb78F4I477qBz5850796dAQMG8NFHH0UZvtRQwgpBTKvpx81ssZntMLPrYpZ/38wWhuu8a2ZjExWLiOyf6lpF33DDDfziF79g9erVHH744cyaNQuA448/nqKiIpYvX84555zD+PHjI34FUhOJPCK4AhgCXA6MASr3jd0JXOvuuUBf4Eoz65zAeERkH1XXKnrBggWcc845AIwYMYKnnnoKgP79+3PooYcC0LdvX9atWxdN4LJPEvKDsthW08C97j7FzHZrDuTunwCfhPe3mtkKoBXw3t6eX/MRxKd++/EpP/Fd220n+TGPy8rK6NWrFx988AFXXnklxx13HE2bNiUrK3j7aN26NevXr9/jeWbNmsXgwYOTE7QckIQUAncfbWanAv3LO4zGY2ZtgeOB1+KsE9uGmpu77aydYNNQywbBP2apmvITX8sGe7ZwmDp1akWr6FatWrF9+/aKdTZu3MjXX3+92zbz589nwYIFTJ06Ne3aQZSUlKTda4q815CZNQIeB8a5+5bq1nP3u4G7IWhDrTbC1VOb5fiUn/iu7baTc6tps7xkyRJ27NjBjh076NevH1lZWSxevJicnJyK1swvvPACTzzxBIsWLaJFixbJCzxJUrUNdTyR/msws4MIisAD7v5ETbdTG+r41GY5PuUnvthPu1W1ir7hhhvo378/jz32GOeffz6zZ8/mjDPOAIJZxC677DLmzZuXlkUgXUVWCCyYiHQWsMLd74gqDhGpXlWtoocOHUrnzp05//zz+dWvfsXxxx/PqFGjALj++uspKSnh3//93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CfV+IbNmwYJ554IqtWraJ169bMmjWLgoIC5s+fT05ODvPnz6egIGgttWLFCrp06UKnTp147rnnmDZN1/NF0s3euo+2cfeP3X0XcE94q025wF/NrIzgG0Sjavn5pQoPPfRQleMvvvjiHmMnnngiq1evTnRIIhKhvV0jeAroCWBmj7v72bWxU/+uDfXn7OfcxyIiUjv2dmoodiLhdokMREREorG3QuDV3BcRkTSxt1NDPcxsC8GRQYPwPuFjd/fGCY1OREQSbm8T09RPViAiIhKNmn59VERE0pQKgYhIhlMhyBD7MgdBYWEhTZo0IS8vj7y8PG677baowhaRJEhoIYiZd+BxM1tsZjvM7LpK65xqZqvM7AMzK0hkPJlsX+YgAPjxj3/M0qVLWbp0KTfffHOywxWRJEr0EUH5vAOXA2OASbELzaw+8GdgMNAZGGZmnRMcU0balzkIRCSzJKyfcKV5B+519ylmVrkhfm/gA3f/Z7jNwwSzob0X77k1H0F8sfMR7M8cBACLFy+mR48eHH300UyaNIkuXbokNmgRiUzCCoG7jzazU4H+7v55Nau1AtbGPF4H9KlqRbWhrrnYNtTxWk/v3Llzt+Xlj7dt28b9999PgwYNePXVVxk0aBD3339/El9BYqVlG+FapPzEl475iXqGEatirMpfMLv73cDdAG3atffJb0cdet11bbedlOeneHh+xXhxcTENGzYkPz8Ya9WqFR07diQ7O5tPPvmEo48+umJZufz8fO666y66du1K8+bNk/QKEquwsHCP1ynfUX7iS8f8RP1uuo5gLuRyrcSwfuMAAA9PSURBVAlmL4urwUH1WRXnlEemKyws3K0AVOf0009n9uzZFBQUMHv2bM444wwgOHJo2bIlZsbrr7/Orl27OOKIIxIctYhEJepC8AaQY2bHAuuB84H/iDak9DRs2DAKCwv5/PPPad26NbfeeisFBQWce+65zJo1izZt2vDoo48C8NhjjzFjxgyysrJo0KABDz/8MOWz04lI+klKITCzo4AioDGwy8zGAZ3dfYuZXQU8D9QnuKj8bjJiyjT7MgfBVVddxVVXXZXokESkjkhoIYiZdwCqmZTe3f9OMOOZiIhEQL8sFhHJcC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d/xqRyOjUkEgNmVm2mb1kZkvN7B0z+3E4fqqZvRn2+H8xHGtmZk+Fcxy8ambdw/FbzOxuM/sH8NewAdztZvZGuO5lEb5EyVA6NSTynQZhV06ANe5+VqXl/0HQfvh34fwYh5rZkcA9wEnuvsbMmoXr3gq85e5nmtlPgL8S9P8H6AX0c/ftZnYpQVuLH5jZIcArZvYPd1+TyBcqEkuFQOQ72z3oylmdN4B7w6Z2T7n7UjPLB14qf+N29/KGY/2As8OxBWZ2hJk1CZc97e7bw/unAN3NrLyPTROCSXJUCCRpVAhEasjdXzKzk4DTgDlmdjvwJVW3HY7XnnhbpfWudvfnazVYkX2gawQiNWRmxxD0qr+HoMNpT2AxcLKZHRuuU35q6CVgeDiWD3zuVc+L8DxweXiUgZl1MLOGCX0hIpXoiECk5vKB682sFCgBLnT3z8Lz/E+YWT2CeQ4GArcAfzGz5cDXfNequLKZBFOPvhm20P4MODORL0KkMn19VEQkw+nUkIhIhlMhEBHJcCoEIiIZToVARCTDqRCIiGQ4FQIRkQynQiAikuH+Pz+xJ2TaYJIRAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "from xgboost import XGBRegressor\n",
    "from xgboost import plot_importance\n",
    "from sklearn.datasets import load_boston\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import mean_squared_error\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "X,y = load_boston(return_X_y=True) # 波士顿房价\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, y)\n",
    "\n",
    "model = XGBRegressor(max_depth=5, \n",
    "                         learning_rate=0.1, \n",
    "                         n_estimators=160, \n",
    "                         silent=True, \n",
    "                         objective='reg:gamma')\n",
    "model.fit(x_train, y_train)\n",
    "\n",
    "# 对测试集进行预测\n",
    "y_pred = model.predict(x_test)\n",
    "mse_predict = mean_squared_error(y_test, y_pred)\n",
    "print(mse_predict)\n",
    "\n",
    "# 显示重要特征\n",
    "plot_importance(model)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调参函数\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "def modify_params(parameters):\n",
    "    #param_grid = {param : param_list}\n",
    "    kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=7)\n",
    "    model = XGBClassifier()\n",
    "    grid_search = GridSearchCV(model, parameters, scoring=\"neg_mean_squared_error\", n_jobs=-1, cv=kfold)\n",
    "    grid_result = grid_search.fit(x_train,y_train)\n",
    "    means = grid_result.cv_results_['mean_test_score']\n",
    "    stds = grid_result.cv_results_['std_test_score']\n",
    "    params = grid_result.cv_results_['params']\n",
    "    for mean, stdev, param in zip(means, stds, params):\n",
    "        print(\"mean:%f ,std:%f with: %r\" % (mean, stdev, param))\n",
    "    print(\"Best: %f using %s\" % (grid_result.best_score_, grid_result.best_params_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean:-0.044643 ,std:0.060176 with: {'learning_rate': 0.0001}\n",
      "mean:-0.044643 ,std:0.060176 with: {'learning_rate': 0.001}\n",
      "mean:-0.044643 ,std:0.060176 with: {'learning_rate': 0.01}\n",
      "mean:-0.035714 ,std:0.043630 with: {'learning_rate': 0.1}\n",
      "mean:-0.035714 ,std:0.043630 with: {'learning_rate': 0.2}\n",
      "mean:-0.044643 ,std:0.060176 with: {'learning_rate': 0.3}\n",
      "Best: -0.035714 using {'learning_rate': 0.1}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/hz/anaconda3/lib/python3.7/site-packages/sklearn/model_selection/_search.py:813: DeprecationWarning: The default of the `iid` parameter will change from True to False in version 0.22 and will be removed in 0.24. This will change numeric results when test-set sizes are unequal.\n",
      "  DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "# 选择学习率\n",
    "learning_rate_param={\"learning_rate\" : [0.0001, 0.001, 0.01, 0.1, 0.2, 0.3]}\n",
    "modify_params(learning_rate_param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
